const app = getApp()
const { getActivityAllList } = require("../../utils/http/api")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    top: Number(app.globalData.menu.top),
    height: Number(app.globalData.menu.height),
    bottom: Number(app.globalData.menu.bottom),
    activityList:[],
    filteredActivityList:[],
    searchValue: '',
    nearAct:{
      id:0,
      adImg:"	https://obs-xhlj.obs.cn-east-3.myhuaweicloud.com/2024/4/660cd4a277ef4e45bd16d696cf1b846a.jpg",
      title:"绿色环保活动安全特训营·数据要素X高级研修班",
      beginTime:"2024-05-17 09:00",
      endTime:"18:00",
      desc:"为加强政企单位数据要素人才队伍建设，增强...",
      type: 0
    },
    activities:[
      {
        id:0,
        adImg:"https://obs-xhlj.obs.cn-east-3.myhuaweicloud.com/2024/4/660cd4a277ef4e45bd16d696cf1b846a.jpg",
        status: 2,
        type: 0,
        beginTime:"2024-05-17 09:00 ",
        endTime:" 18:00",
        title:"绿色环保活动安全特训营·数据要素X高级研修班"
      },{
        id:0,
        adImg:"https://obs-xhlj.obs.cn-east-3.myhuaweicloud.com/2024/4/cd1d676212094d579358b8d3b3011351.jpg",
        status: 2,
        type: 0,
        beginTime:"2024-05-17 09:00 ",
        endTime:" 18:00",
        title:"绿色环保活动安全特训营·AI新质生产力-大模型认知与应用能力提升班"
      },{
        id:0,
        adImg:"https://obs-xhlj.obs.cn-east-3.myhuaweicloud.com/2024/4/59297246f86544acba61e64faf27456e.jpg",
        status: 2,
        type: 1,
        beginTime:"2024-04-19 09:30 ",
        endTime:" 12:25",
        title:"AI·引领 智·启新程"
      },{
        id:0,
        adImg:"https://obs-xhlj.obs.cn-east-3.myhuaweicloud.com/2024/4/b83667da136043888da66a03f71d3445.jpg",
        status: 0,
        type: 1,
        beginTime:"2024-04-03 00:00 ",
        endTime:" 00:00",
        title:"十一周年精华特刊"
      },{
        id:0,
        adImg:"https://obs-xhlj.obs.cn-east-3.myhuaweicloud.com/2024/4/2c6404455065444abad01321d331e2ed.jpg",
        status: 0,
        type: 0,
        beginTime:"2024-03-11 00:00 ",
        endTime:" 23:00",
        title:"2024绿色环保活动丨数据安全创新实践案例征集启动"
      },{
        id:0,
        adImg:"https://obs-xhlj.obs.cn-east-3.myhuaweicloud.com/2023/4/ad60963eb139452bbf688382882cf55b.png",
        status: 0,
        type: 1,
        beginTime:"2023-04-12 ",
        endTime:"  ",
        title:"绿色环保活动的十一个维度"
      },
    ]
  },
  // 搜索输入变化
  onChange(e) {
    this.setData({
      searchValue: e.detail
    })
    this.filterActivityList(e.detail)
  },

  // 搜索事件
  onSearch(e) {
    this.setData({
      searchValue: e.detail
    })
    this.filterActivityList(e.detail)
  },

  // 过滤活动列表
  filterActivityList(searchValue) {
    const { activityList } = this.data
    if (!searchValue || searchValue.trim() === '') {
      this.setData({
        filteredActivityList: activityList
      })
      return
    }
    
    const filtered = activityList.filter(item => {
      return item.activityTitle.toLowerCase().includes(searchValue.toLowerCase()) ||
             (item.venues && item.venues.venuesName && item.venues.venuesName.toLowerCase().includes(searchValue.toLowerCase()))
    })
    
    this.setData({
      filteredActivityList: filtered
    })
  },

  // 活动详情页
  goActiveDetail(e){
    console.log("跳转活动详情页面",e)
    let item = e.currentTarget.dataset.item
    if(item.activityType == 0){
      wx.navigateTo({
        url: '../index/live/live?activityId='+ item.activityId,
      })
    }else{
      wx.navigateTo({
        url: '../activeDetail/activeDetail?url='+ item.activityImage,
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let _this = this
    _this.getActivityAllList()
  },
  getActivityAllList(){
    let _this = this;
    getActivityAllList().then((res) => {  
      if (res && res.rows) {  // 确保res和res.data都存在  
        const activityList = res.rows;  
        console.log("活动列表", activityList); 
        activityList.forEach((item)=>{
          let image = item.activityImage.split(",")
          item.activityImageFirst = image[0]
          const collectCount = item.xhljTopicActivityCollectVoList.filter(subItem => subItem.isCollect == '1').length;
          // 将计算的数量赋值给 collectCount
          console.log(collectCount)
           item.collectCount = collectCount;
        })
        _this.setData({  
          activityList: activityList,
          filteredActivityList: activityList,
          nearAct: activityList[4]
        });
        
      }  
    }).catch((error) => {  
      console.error('请求活动详情失败:', error);  
    });  
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})